package com.neutech.neupha.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.neutech.neupha.mapper.DosageMapper;
import com.neutech.neupha.mapper.DrugsMapper;
import com.neutech.neupha.mapper.PhaMapper;
import com.neutech.neupha.service.PhaService;
import com.neutech.neupha.vo.ToPhaVo;
import com.neutech.neupha.vo.ViewData;
import com.neutech.po.Constantitem;
import com.neutech.po.NeuPha;

@Service
public class PhaServiceImpl implements PhaService{
	@Autowired
	private PhaMapper phaMapper;
	@Autowired
	private DosageMapper dosageMapper;
	@Autowired
	private DrugsMapper drugsMapper;

	/**
	 * 增加药品
	 * @param: pha
	 * @return: 成功"1" 失败"0"
	 */
	@Override
	public int addPha(NeuPha neuPha) {
		int phaNum = phaMapper.addPha(neuPha);
		if(phaNum == 1){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public int deletePha(NeuPha neuPha) {
		int phaNum = phaMapper.deletePha(neuPha);
		if(phaNum == 1){
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public void updatePha(NeuPha neuPha) {
		phaMapper.updatePha(neuPha);
	}

	@Override
	public ToPhaVo toPhaVo() {
		List<Constantitem> dosageList = dosageMapper.queryDosage();
		List<Constantitem> drugsList = drugsMapper.queryDrugs();
		
		ToPhaVo toPha = new ToPhaVo();
		toPha.setDosageList(dosageList);
		toPha.setDrugsList(drugsList);
		return toPha;
	}

	@Override
	public List<NeuPha> queryPha() {
		return phaMapper.queryPha();
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int addNewPha(Map<String, Object> drugs) {
		return phaMapper.addNewPha(drugs);
	}

	@Override
	public ViewData getAllDrugs(Map<String, Object> map) {
		String pageNum = (String)map.get("pageNumber");
		try{
			Integer i = new Integer(pageNum);
			i = i - 1;
			if(i < 0){
				i = 0;
			}
			i = i * 10;
			map.put("pageNumber",i);
		}catch(Exception e){
			map.put("pageNumber", 0);
		}
		ViewData vd = new ViewData();
		vd.setMap(phaMapper.getAllDrugs(map));
		vd.setPageNumber(phaMapper.getDrugsCount(map));
		return vd;
	}

}
